import eventQueue from 'common/event-queue'
import { getItem } from 'common/storage'
import axios from 'axios'
import apiUrls from 'common/api-urls'
import { decorateMenuList } from 'components/menus/models'
import menuData from 'common/menu'

export function getUserInfoFromCache() {
  const userInfo = getItem('userInfo')
  if (userInfo) {
    return userInfo
  } else {
    return {}
  }
}

export function runEventQueue(route) {
  eventQueue.executeQueue(route)
}

export function clearQueue() {
  eventQueue.clearQueue()
}

export function getPermissionList(params) {
  const defaultParams = {
    belongType: 1
  }

  params = Object.assign(defaultParams, params)

  return axios.get(apiUrls.getPermissionList, {
    params: {
      ...params
    }
  })
}

export function getIsAdmin() {
  return axios.get(apiUrls.isAdmin)
}

// export function getMenuListAndPermision() {
//   let resolveCb = null
//   let rejectCb = null
//   const promise = new Promise((resolve, reject) => {
//     resolveCb = resolve
//     rejectCb = reject
//   })

//   Promise.all([
//     getPermissionList(),
//     getLeftMenuList(),
//     getIsAdmin()
//   ]).then(res => {
//     const permissions = res[0]['data']['permissions']
//     const rawMenuList = res[1]['data']
//     const isAdmin = res[2]['data']
//     resolveCb({
//       menuList: decorateMenuList(rawMenuList),
//       permissions,
//       isAdmin
//     })
//   }).catch(err => {
//     console.error('getMenuListAndPermision err:', err)
//     rejectCb(err)
//   })

//   return promise
// }

// export function getMenuListAndPermision() {
//   let resolveCb = null
//   let rejectCb = null
//   const promise = new Promise((resolve, reject) => {
//     resolveCb = resolve
//     rejectCb = reject
//   })

//   Promise.all([
//     getLeftMenuList(),
//   ]).then(res => {
//     // const rawMenuList = traverGroupTree(res[0]['data'] ? (res[0]['data'].children || []) : [])
//     const rawMenuList = res[0]['data'] ? (res[0]['data'].children || []) : []
//     resolveCb({
//       menuList: rawMenuList,
//       permissions: [],
//       isAdmin: true
//     })
//   }).catch(err => {
//     console.error('getMenuListAndPermision err:', err)
//     rejectCb(err)
//   })

//   return promise
// }

// 静态
export function getMenuListAndPermision() {
  return new Promise((resolve) => {
    resolve({
      menuList: decorateMenuList(menuData.data),
      permissions: [],
      isAdmin: true
    })
  })
}
